package com.freeman.cement.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.freeman.cement.entity.ResponseResult;
import com.freeman.cement.exception.AppException;
import com.freeman.cement.exception.AppExceptionCodeMsg;
import com.freeman.cement.mapper.ShiftDao;
import com.freeman.cement.mapper.StaffDao;
import com.freeman.cement.entity.ShiftRecord;
import com.freeman.cement.entity.Staff;
import com.freeman.cement.myenum.UsrRole;
import com.freeman.cement.service.ShiftService;
import com.freeman.cement.service.StaffService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ShiftServiceImpl implements ShiftService {
    @Resource
    private StaffDao staffDao;
    @Resource
    private ShiftDao shiftDao;
    @Resource
    private StaffService staffService;

    private Date dateStringToDate(String time) {
        Date date = null;
        if (StringUtils.isNotBlank(time)) {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date = simpleDateFormat.parse(time);
            } catch (Exception e) {
                throw new AppException(AppExceptionCodeMsg.TIME_STRING_FORMAT_IS_WRONG);
            }
        }
        return date;
    }

    //提请交班
    @Override
    public ResponseResult submitOffDuty(int userId, int dutyRoomId, String memo) {
        if(userId > 0 && dutyRoomId > 0) {
            Staff staff = staffDao.selectById(userId);
            if (staff != null && (staff.getRoleId() == UsrRole.DUTY.getIndex() || staff.getRoleId() == UsrRole.ADMIN.getIndex())) {
                //先记录交班信息
                ShiftRecord record = new ShiftRecord();
                //record.setDutyid("jb_" + staff.getDeptId() + "_" + (new Date().getTime()));
                record.setDeptid(dutyRoomId);
                record.setShiftstaff(staff.getId());
                record.setShifttime(new Date());
                record.setMemo(memo);
                //shiftDao.addShiftRecord(record);
                if (shiftDao.insert(record) > 0) {
                    //再退出登录
                    staffService.UserLogout(staff.getId());
                    return ResponseResult.successMsg("交班成功");
                } else {
                    throw new AppException(AppExceptionCodeMsg.ADD_SHIFT_RECORD_ERROR);
                }
            } else {
                throw new AppException(AppExceptionCodeMsg.NO_AUTHORITY_OF_DUTY);
            }
        } else {
            throw new AppException(AppExceptionCodeMsg.ID_IS_NULL);
        }
    }

    //提请接班
    @Override
    public ResponseResult submitOnDuty(int userId, int dutyRoomId) {
        if (userId > 0 && dutyRoomId > 0) {
            Staff staff = staffDao.selectById(userId);
            if (staff != null && (staff.getRoleId() == UsrRole.DUTY.getIndex() || staff.getRoleId() == UsrRole.ADMIN.getIndex())) {
                ShiftRecord record = shiftDao.findLatestShiftRecordByDutyRoomId(dutyRoomId);
                if (record != null) {
                    ShiftRecord _record = new ShiftRecord();
                    _record.setId(record.getId());
                    _record.setSuccessor(userId);
                    _record.setSuccesstime(new Date());
                    if (shiftDao.updateById(_record) > 0) {
                        return ResponseResult.successMsg("接班成功");
                    } else {
                        throw new AppException(AppExceptionCodeMsg.UPDATE_SHIFT_RECORD_ERROR);
                    }
                } else {
                    throw new AppException(50022, "提请接班错误，无匹配的记录");
                }
            } else {
                throw new AppException(AppExceptionCodeMsg.NO_AUTHORITY_OF_DUTY);
            }
        } else {
            throw new AppException(AppExceptionCodeMsg.ID_IS_NULL);
        }
    }

    @Override
    public ResponseResult selectShiftRecord(ShiftRecord shiftRecord, String startTime, String endTime, int pageNo, int pageSize) {
        Date start = null;
        Date end = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //验证分页范围
        if (pageNo < 1 || pageSize > 1000) {
            throw new AppException(AppExceptionCodeMsg.PAGE_NO_OR_SIZE_WRONG);
        }
        //验证开始时间
        if (StringUtils.isNotBlank(startTime)) {
            start = dateStringToDate(startTime);
        }
        //验证结束时间
        if (StringUtils.isNotBlank(endTime)) {
            end = dateStringToDate(endTime);
        }

        PageHelper.startPage(pageNo, pageSize, true);
        List<ShiftRecord> records = shiftDao.selectShiftRecord(shiftRecord, start, end);
        PageInfo<ShiftRecord> pageInfo = new PageInfo<ShiftRecord>(records);
        return ResponseResult.success(pageInfo);
    }
}
